<template>
  <div>
    <van-nav-bar
      :title="`${$route.query.q} 的搜索结果`"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
      placeholder
    />
    <!-- 搜索结果显示 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" @click="$router.push({path: '/article', query: {id: item.art_id}})"/>
    </van-list>
  </div>
</template>

<script>
import { getSearchResults } from "./home.js";
export default {
  name: "searchResult",
  data() {
    return {
      loading: false,
      finished: false,
      list: [],
      page: 1,
    };
  },
  methods: {
    async onLoad() {
      //   加载数据
      let res = await getSearchResults({
        page: this.page,
        pre_page: 10,
        q: this.$route.query.q,
      });
      //   console.log(res)
      // 添加数据到list
      this.list.push(...res.data.data.results);
      // 加载下一页的数据
      this.page++;
      // 加载状态结束
      this.loading = false;

      // 数据全部加载完成
      if (this.list.length >= 40) {
        this.finished = true;
      }
    },
  },
};
</script>

<style></style>
